import { constantRoutes } from '@/router'



function hasPermission(roles, route) {
    if (route.meta && route.meta.roles) {
        return roles.some(role => route.meta.roles.includes(role))
    } else {
        return true
    }
}

export function filterAsyncRoutes(routes, roles) {
    const res = []

    routes.forEach(route => {
        const tmp = {...route }
        if (hasPermission(roles, tmp)) {
            if (tmp.children) {
                tmp.children = filterAsyncRoutes(tmp.children, roles)
            }
            res.push(tmp)
        }
    })
    return res
}

const state = {
    // routes: [],
    routes: constantRoutes,

}

const mutations = {
    SET_ROUTES: (state, routes) => {


    }
}

const actions = {

}

export default {
    namespaced: true,
    state,
    mutations,
    actions
}



// export const asyncRoutes = [{



// {
//     path: '/error',
//     component: Layout,
//     redirect: 'noRedirect',
//     name: 'ErrorPages',
//     meta: {
//         title: 'Error Pages',
//         icon: '404'
//     },
//     children: [{
//             path: '401',
//             component: () =>
//                 import ('@/views/error-page/401'),
//             name: 'Page401',
//             meta: { title: '401', noCache: true }
//         },
//         {
//             path: '404',
//             component: () =>
//                 import ('@/views/error-page/404'),
//             name: 'Page404',
//             meta: { title: '404', noCache: true }
//         }
//     ]
// },








// {
//     path: '/clipboard',
//     component: Layout,
//     children: [{
//         path: 'index',
//         component: () =>
//             import ('@/views/clipboard/index'),
//         name: 'ClipboardDemo',
//         meta: { title: 'Clipboard', icon: 'clipboard' }
//     }]
// },

// {
//     path: 'external-link',
//     component: Layout,
//     children: [{
//         path: 'https://github.com/PanJiaChen/vue-element-admin',
//         meta: { title: 'External Link', icon: 'link' }
//     }]
// },

// // 404 page must be placed at the end !!!
// { path: '*', redirect: '/404', hidden: true }
// ]